<template>
    <rui-page :defines="defines">
        <rui-form :form-options="searchFormOptions"
                  :label-width="300"
                  :read-only="false"
                  ref="sourceForm">
        </rui-form>
        下面的参数选择之后，进入上面的规则表达式，表达式具体操作符包含：+、-、*、/、（、）。
        <rui-table
            :defineId="'defineTable'"
            :showIndex="false"
            :select="false"
            :columns="['key','name']"
            @loadDatas="parameterData"
            @on-row-dblclick="handleRowChange"
            ref="parameterTable"
        >
        </rui-table>
        <Row style="text-align: center;">
            <Button @click="save" icon="md-bookmark" type="primary">保存</Button>
            <Button @click="close" icon="md-backspace" type="primary">返回</Button>
        </Row>
    </rui-page>
</template>
<script>
    import tableDefine from "./define/discount-source-rule-define";
    import {
        saveSource
    } from "@/projects/afs-core-business/api/afs-product/product/discountSourceRule";
    import {queryAtomsByBizType} from "@/projects/basic/api/rule/ruleAtom";

    export default {
        data() {
            return {
                isShow: true,
                ruleTitle: null,
                modalType: 0,
                fitScope: '001',
                searchFormOptions: [
                    {
                        isGroup: false,
                        grids: [
                            {defineId: "defineTable", span: 9, fields: ["discountSourceName"]},
                            {defineId: "defineTable", span: 9, fields: ["ruleExpression"]}
                        ]
                    }
                ],
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "defineTable",
                        fields: tableDefine
                    }
                ];
            }
        },
        methods: {
            sourceCom(row) {
                if (row == null) {
                    this.isShow = true;
                    this.$refs.sourceForm.setFieldRequired('discountSourceName', true);
                    this.$refs.sourceForm.setFieldRequired('ruleExpression', true);
                    this.$nextTick(() => {
                        this.$refs.sourceForm.resetFrom();
                    })
                } else {
                    this.isShow = false;
                    this.$refs.sourceForm.resetFrom();
                    this.$refs.sourceForm.updateFormData(row);
                    this.$refs.sourceForm.setFieldRequired('discountSourceName', true);
                    this.$refs.sourceForm.setFieldRequired('ruleExpression', true);
                    this.parameterData();
                }
            },
            refresh() {
                this.$refs['parameterTable'].reloadData();
            },
            parameterData(pageNumber = 1, pageSize = 10) {
                queryAtomsByBizType(this.fitScope,'product').then(res => {
                    if (res.code === "0000") {
                        this.$refs.parameterTable.updateTableData(res.data);
                    }
                });
            },
            handleRowChange(currentRow) {
                this.$refs.sourceForm.setFiledValue("ruleExpression", this.$refs.sourceForm.getFormData().ruleExpression +
                    '#{' + currentRow.key + '}');
            },
            close() {
                this.$emit('next', "退回");
            },
            save() {
                this.$refs.sourceForm.getForm().validate((valid) => {
                    if (valid) {
                        saveSource(this.$refs.sourceForm.getFormData()).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success(res.msg);
                                this.refresh();
                                if ("规则填写不正确" !== res.msg) {
                                    this.$emit('next', "退回");
                                }
                            }
                        });
                    } else {
                        this.$Message.error('数据不完整!');
                    }
                })
            },
        },
        mounted() {
            this.refresh();
        }
    };
</script>
